require 'find'

class ModelControllerMethodList
  def initialize(project_path)
    @models = []
    @controllers = []
    @model_methods = []
    @controller_methods = []
    list(project_path)
  end
  
  def list(project_path)
    get_models(project_path)
    get_controllers(project_path)
  end 
  
  def get_models(project_path)
    cnt = 0
    Find.find(project_path.to_s + 'app/models/') do |f|
      final_methods = []
      if f.split('.').last == 'rb'
        @models << f.split('/').last.split('.').first.capitalize 
        File.open(f,'r').each do |line|
          method_crude = line.match(/def (.*\()/)
          if method_crude != nil
            #~ p @models.last
            final_methods << method_crude.to_s.split(' ')[1].split('.').last.delete('(')
          end
          #~ #@methods << {:model => @models.last, :method => line.match(/def (*.)/)
        end
        @model_methods << {:model => @models.last, :methods => final_methods}
      end
     cnt += 1
    end
  end
  
  def get_controllers(project_path)
    cnt = 0
    Find.find(project_path.to_s + 'app/controllers/') do |f|
      final_methods = []
      if f.split('.').last == 'rb'
        @controllers << f.split('/').last.split('.').first.split('_').first.capitalize  if f.split('.').last == 'rb'
        File.open(f,'r').each do |line|
          method_crude = line.match(/def (.*\()/)
          if method_crude != nil
            #~ p @models.last
            final_methods << method_crude.to_s.split(' ')[1].split('.').last.delete('(')
          end
          #~ #@methods << {:model => @models.last, :method => line.match(/def (*.)/)
        end
        @controller_methods << {:controller => @controllers.last, :methods => final_methods} if final_methods != nil
      end
      cnt += 1
    end
  end
  
  attr_accessor :models, :controllers, :model_methods, :controller_methods
end

def printhash(hash)
  puts '----------------------------------------------------'
  hash.each do |key,value|
    if value.class.to_s == 'Array'
      value = value.join(', ')
    end
    puts "#{key} => #{value}"
  end
end

def printarr(arr)
  arr.each do |a|
    if a.class.to_s == 'Hash'
      printhash(a) 
    else
      puts a
    end
  end
  puts '----------------------------------------------------'
end


m = ModelControllerMethodList.new('E:/workspace/yellowpages/lui/')
#m.list('E:/workspace/yellowpages/lui/')
puts '-----------Models-----------'
puts m.models
puts '---------Controllers--------'
puts m.controllers
puts '--------Model Methods-------'
printarr(m.model_methods)
puts '-----Controller Methods-----'
printarr(m.controller_methods)